from collections import namedtuple
import logging
import copy
from PyQt5.QtWidgets import QWidget

CoupleNum = namedtuple("CoupleNum", ["x", "y"])
TripleNum = namedtuple("TripleNum", ["x", "y", "z"])

def switch_namedtuple2str(obj_namedtuple):
    if isinstance(obj_namedtuple, CoupleNum):
        ret_str = str.format("%d,%d" % (obj_namedtuple[0], obj_namedtuple[1]))
    elif isinstance(obj_namedtuple, TripleNum):
        ret_str = str.format("%d,%d,%d" % (obj_namedtuple[0], obj_namedtuple[1], obj_namedtuple[2]))
    else:
        # try:
        ret_str = str(obj_namedtuple)
    return ret_str

def switch_str2namedtuple(struct_type, str_value):
    if struct_type == CoupleNum:
        list_str = str_value.split(',')
        ret_obj = CoupleNum(int(list_str[0]), int(list_str[1]))
    elif struct_type == TripleNum:
        list_str = str_value.split(',')
        ret_obj = TripleNum(int(list_str[0]), int(list_str[1]), int(list_str[2]))
    else:  # 其他类型，如QWidget，按字符处理
        ret_obj = str_value
    return ret_obj

# widget atrribute: 必选项
Dict_Widget_attrtype_common = {
    "name": str,
    "type": QWidget,
    "pos" : CoupleNum,
    "size": CoupleNum
}

# widget attribute: 可选属性集
Dict_Widget_attrtype_unique = {
    "PushButton":{
        "click"     : bool,
        "comment"   : str
    },
    "RadioButton":{
        "comment"   : str
    },
    "Text":{
        "comment"   : str
    }
}

# widget 图元类别
Set_Widgets_type = set(Dict_Widget_attrtype_unique.keys())

# 图元的通用属性集，每个图元都需要包含该列表中的属性项
Set_Widgets_attrib_common = Dict_Widget_attrtype_common.keys()
# 全部图元的特殊属性集
Set_Widgets_attrib_unique = set()
for widget in Dict_Widget_attrtype_unique.values():
    Set_Widgets_attrib_unique.update(widget.keys())

# 全部图元属性
Set_Widgets_attrib = copy.copy(Set_Widgets_attrib_unique)
Set_Widgets_attrib.update(Set_Widgets_attrib_common)

##########################################################
# xml 各个类别的属性集
Dict_Xml_describe = {
    "window":{
        "name", "default_page", "size"
    },
    "plugin":{
        "file", "path"
    },
    "page":{
        "name", "size", "bkgpng"
    },
    "widget": Set_Widgets_attrib
}

Set_Xml_type = set(Dict_Xml_describe.keys())

# dict_all_elem = {
#     "window": {
#         "name":"xxx",
#         "size":"ttt",
#         "default_page":"yyy"
#     },
#     "pages":[
#         {
#             "name": "main",  # 不能为空
#             "widgets":[
#                 {
#                     # "id": num,  # 用id标识唯一元素，但该条目并不存在于xml，而是xml解析时自动添加
#                     "name": "",
#                     "type": "",
#                     "size": "200,300",
#                     "position": "400,500"
#                 },
#                 {
#                     "name": "",
#                     "type": ""
#                 }
#             ]
#         },
#         {page2...}
#     ],
#     "plugins": ["xxx/y.so", ""]
# }


## Functions Define ##
LOG_LEVEL = logging.DEBUG
def make_logging(name, level = None, output_file = None):
    if level is None:
        level = LOG_LEVEL
    logger = logging.getLogger(name)
    logger.setLevel(level)
    if output_file is None:
        handler = logging.StreamHandler()
    else:
        handler = logging.FileHandler(output_file)
    handler.setLevel(level)
    # formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger
